﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class ComradeCreate : Form
    {
        public int c_id;
        public int pr_id;
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;

        public ComradeCreate()
        {
            InitializeComponent();
        }

        private void ComradeCreate_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select pr_id as [ردیف],pr_fname as [نام],pr_lname as [نام خانوادگی] , pr_fathername as [نام پدر] ,pr_ncode as [شماره ملی], pr_relationship as [نسبت] from Person WHERE pr_c_id =@cid order by pr_id desc", Cnn);
            da.SelectCommand.Parameters.AddWithValue("@cid", c_id);

            da.Fill(ds);
            dGView_Comrade.DataSource = ds.Tables[0];
            dGView_Comrade.Columns[0].Visible = false;
        }

        private void BtnComradeSave_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(fnameTxt.Text)&&!string.IsNullOrEmpty(lnameTxt.Text)&&!string.IsNullOrEmpty(fathernameTxt.Text)&&!string.IsNullOrEmpty(ncodeTxt.Text)&&!string.IsNullOrEmpty(RelationShipTxt.Text))
            {
                SqlCommand SelectPerson = new SqlCommand("SELECT pr_ncode FROM Person WHERE pr_ncode=@pr_ncode", Cnn);
                SelectPerson.Parameters.AddWithValue("@pr_ncode", ncodeTxt.Text.Trim());
                Cnn.Open();
                SqlDataReader dr = SelectPerson.ExecuteReader();
                bool hasrows = dr.HasRows;
                Cnn.Close();

                if (hasrows)
                {
                    MessageBox.Show("این کد ملی قبلا در سیستم ثبت شده است");
                }

                else
                {

                    SqlCommand SaveINDB = new SqlCommand("INSERT INTO Person(pr_c_id,pr_fname,pr_lname,pr_fathername,pr_ncode,pr_relationship) values (@c_id,@fname,@lname,@fathername,@ncode,@relationship)", Cnn);

                    SaveINDB.Parameters.AddWithValue("@c_id", c_id);
                    SaveINDB.Parameters.AddWithValue("@fname", fnameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@lname", lnameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@fathername", fathernameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@ncode", ncodeTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@relationship", RelationShipTxt.Text.Trim());

                    Cnn.Open();
                    SaveINDB.ExecuteNonQuery();
                    Cnn.Close();

                    ds.Clear();
                    da.Fill(ds);
                    dGView_Comrade.DataSource = ds.Tables[0];

                    fnameTxt.Text = "";
                    lnameTxt.Text = "";
                    fathernameTxt.Text = "";
                    ncodeTxt.Text = "";
                    RelationShipTxt.Text = "";
                }
            }
            else
            {
                MessageBox.Show("لطفا همه قسمت ها را به درستی تکمیل نمایید");
            }


            
        }

        private void OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void BtnComradeDelete_Click(object sender, EventArgs e)
        {
            SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Person WHERE pr_id=@pr_id", Cnn);
            DeleteCMD.Parameters.AddWithValue("@pr_id", pr_id);
            Cnn.Open();
            DeleteCMD.ExecuteNonQuery();
            Cnn.Close();

            ds.Clear();
            da.Fill(ds);
            dGView_Comrade.DataSource = ds.Tables[0];

            BtnComradeDelete.Enabled = false;
            BtnComradeEdit.Enabled = false;
            fnameTxt.Text = "";
            lnameTxt.Text = "";
            ncodeTxt.Text = "";
            fathernameTxt.Text = "";
            RelationShipTxt.Text = "";
        }

        private void BtnComradeEdit_Click(object sender, EventArgs e)
        {
            SqlCommand UpdateCMD = new SqlCommand("update Person set pr_fname=@fname , pr_lname=@lname , pr_fathername=@fathername ,pr_ncode=@ncode , pr_relationship=@relationship WHERE pr_id=@pr_id", Cnn);

            UpdateCMD.Parameters.AddWithValue("@pr_id", pr_id);
            UpdateCMD.Parameters.AddWithValue("@fname", fnameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@lname", lnameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@fathername", fathernameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@ncode", ncodeTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@relationship", RelationShipTxt.Text.Trim());

            Cnn.Open();
            UpdateCMD.ExecuteNonQuery();
            Cnn.Close();

            ds.Clear();
            da.Fill(ds);
            dGView_Comrade.DataSource = ds.Tables[0];


            BtnComradeDelete.Enabled = false;
            BtnComradeEdit.Enabled = false;
            fnameTxt.Text = "";
            lnameTxt.Text = "";
            ncodeTxt.Text = "";
            fathernameTxt.Text = "";
            RelationShipTxt.Text = "";
        }

        private void dGView_Comrade_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            fnameTxt.Text = dGView_Comrade.CurrentRow.Cells[1].Value.ToString();
            lnameTxt.Text = dGView_Comrade.CurrentRow.Cells[2].Value.ToString();
            fathernameTxt.Text = dGView_Comrade.CurrentRow.Cells[3].Value.ToString();
            ncodeTxt.Text = dGView_Comrade.CurrentRow.Cells[4].Value.ToString();
            RelationShipTxt.Text = dGView_Comrade.CurrentRow.Cells[5].Value.ToString();

            pr_id = (int)dGView_Comrade.CurrentRow.Cells[0].Value;


            BtnComradeEdit.Enabled = true;
            BtnComradeDelete.Enabled = true;
        }



    }
}
